<?php

class news_module {

	public function page($page, $number)
	{
		$start = (($page - 1) * $number);
		
		$query = new Query("SELECT * FROM `news` ORDER BY `date` DESC LIMIT ?, ?", $start, $number);
		
		if($query->num_rows() > 0)
		{
			$news = news::RecordSet($query, 'news');
			
			#display these news entries
			$show .= self::display($news);
		}
		else
		{
			$show = "There are currently no news entries.";
		}
		
		return $show;
	}
	
	public function display($newsRS)
	{
		foreach($newsRS as $news)
		{
			$show .= display::headerLink($news->data['title'], 'news/' . $news->data['url']) . 
					 display::dateHeader($news->data['date']) . 
					 format::short($news->data['content']) . '
					 <p>' . display::link('Read Full Story', 'news/' . $news->data['url']) . '</p>';
		}
		
		return $show;
	}
	
	public function archiveNews($number = 3)
	{
		$query = new Query("SELECT * FROM `news` WHERE `date` >= ? ORDER BY `date` DESC LIMIT 0, ?", mktime(0,0,0,1,1,date('Y',time())),$number);
		
		if($query->num_rows() > 0)
		{
			$news = news::RecordSet($query, 'news');
			
			#display these news entries
			$show .= self::display($news);
		}
		else
		{
			$show = "There are currently no news entries for the current year.";
		}
		
		return $show;
	}
	
	public function archive($year, $month = 0)
	{
		if($month ==  0)
		{
			//find every month that has a news entry, display that
			$query = new Query("SELECT * FROM `news` WHERE `date` >= ? and `date` <= ? ORDER BY `date` DESC", mktime(0, 0, 0, 1, 1, $year), mktime(0, 0, 0, 12, 31, $year));
			if($query->num_rows() > 0)
			{
			
				$news = news::recordSet($query, 'news');
					
				return self::display($news);
			}
			else
			{
				return 'No News Entries Found';
			}
		
		}	
		else
		{
			// a specific month is set
			if($month == 1)
			{
				$query = new Query("SELECT * FROM `news` WHERE `date` > ? AND `date` <= ? ORDER BY `date` DESC", mktime(0, 0, 0, 12, 31, $year - 1), mktime(0, 0, 0, 1, 31, $year));			
			}
			else if ($month == 12)
			{
				$query = new Query("SELECT * FROM `news` WHERE `date` >= ? AND `date` < ? ORDER BY `date` DESC", mktime(0, 0, 0, 12, 1, $year), mktime(0, 0, 0, 1, 1, $year + 1));
			}
			else
			{
				$query = new Query("SELECT * FROM `news` WHERE `date` >= ? AND `date` < ? ORDER BY `date` DESC", mktime(0, 0, 0, $month, 1, $year), mktime(0, 0, 0, $month+1, 1, $year));
			}
			
			if($query->num_rows() > 0)
			{
			
				$news = news::recordSet($query, 'news');
					
				return self::display($news);
			}
			else
			{
				return 'No News Entries Found';
			}
		}
	}
	
	//get the years that have previous entries
	public function archiveYears()
	{
		$year = date('Y', time());
		
		$hasNextYear = true;
		
		while($hasNextYear)
		{
			$query = new Query("SELECT * FROM `news` WHERE `date` < ? AND `date` >= ? ORDER BY `id`", mktime(0,0,0,1,1,$year), mktime(0,0,0,1,1,$year - 1));
			if($query->num_rows() > 0)
			{
				$show .= display::headerLink($year - 1, 'news/archive/' . ($year - 1));
			}
			
			$query = new Query("SELECT * FROM `news` WHERE `date` < ? ORDER BY `id`", mktime(0,0,0,1,1,$year - 1));
			if($query->num_rows()>0)
			{
				$year--;
			}
			else
			{
				$hasNextYear = false;
			}
			
		}
		
		return $show;
	}
	

}
?>